package com.xiaoyu.client.ui.activity.nearby;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;

import com.techfansy.bottomDialog.BottomDialog;
import com.xiaoyu.client.R;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jx
 * @date 创建时间 2018/8/24
 * @Description 调取手机上存在的地图(地址)
 */
public class TransferMap {

    private Context mContext;
    private String mCoordinate;
    private Intent intent = new Intent();

    public TransferMap(Context mContext, String mCoordinate) {
        this.mContext = mContext;
        this.mCoordinate = mCoordinate;
        if (!TextUtils.isEmpty(mCoordinate)) {
            setMap();
        }
    }

    //设置地图弹窗
    private void setMap() {

        new BottomDialog(mContext).addItemSheet(R.string.badidu_map, new BottomDialog.SheetListener() {
            @Override
            public void setOnSheet(int i) {
                baiduMap();
            }
        }).addItemSheet(R.string.autonavi_map, new BottomDialog.SheetListener() {
            @Override
            public void setOnSheet(int i) {
                autonaviMap();
            }
        }).setCancel(new BottomDialog.SheetListener() {
            @Override
            public void setOnSheet(int i) {
            }
        }).show();
    }

    //百度地图
    private void baiduMap() {
        if (isAvilible(mContext, "com.baidu.BaiduMap")) {//传入指定应用包名

            try {
                Intent i1 = new Intent();
                i1.setData(Uri.parse("baidumap://map/geocoder?src=openApiDemo&address=" + mCoordinate));
                mContext.startActivity(i1);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {//未安装
            //market为路径，id为包名
            //显示手机上所有的market商店
            ToastUtil.showToast(mContext, "您尚未安装百度地图");
            Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
            intent = new Intent(Intent.ACTION_VIEW, uri);
            mContext.startActivity(intent);
        }
    }

    //高德地图
    private void autonaviMap() {

        if (isAvilible(mContext, "com.autonavi.minimap")) {
            try {

                String act = "android.intent.action.VIEW";
                String dat = "androidamap://keywordNavi?sourceApplication=softname&keyword=" + mCoordinate + " &style=2";
                String pkg = "com.autonavi.minimap";
                Intent intent = new Intent(act, Uri.parse(dat));
                intent.setPackage(pkg);
                mContext.startActivity(intent);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ToastUtil.showToast(mContext, "您尚未安装高德地图");
            Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
            intent = new Intent(Intent.ACTION_VIEW, uri);
            mContext.startActivity(intent);
        }

    }


    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName 应用包名
     * @return
     */
    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }
}
